#!/bin/bash
while  [ true ]
do
  # 查看flume进程是否正在运行
  FPID=$(ps -ef  | grep flume | grep -v grep | grep  -v flume_listen |  awk  '{print $2}')
  echo  $FPID
  if [ ${FPID} ]
  then  echo  "flume进程正在运行... ..."
  # 请求 flume提供的监控服务
 EventDrainSuccessCount1=$(echo $(curl  http://doitedu01:34545/metrics) | jq -r  .'"SINK.k1"'.EventDrainSuccessCount)
  # 根据指标判断是否重启flume采集
  sleep  300
  EventDrainSuccessCount2=$(echo $(curl  curl http://doitedu01:34545/metrics) | jq -r   .'"SINK.k1"'.EventDrainSuccessCount)
 if  [  $EventDrainSuccessCount1 -eq   $EventDrainSuccessCount2 ]
 # 可能有问题
 then
   kill  -9  $FPID
   sleep  10
   # 重启
   nohup /opt/apps/flume-1.9.0/flume-ng  agent  -n a1  -c  /opt/apps/flume-1.9.0/conf/ -f  /opt/apps/flume-1.9.0/agents/04_demo.prop   -Dflume.monitoring.type=http -Dflume.monitoring.port=34545  1>/dev/null  2>&1  &
 fi
  else  echo "flume不存在了... ..."
  # 启动flume采集
  nohup /opt/apps/flume-1.9.0/flume-ng  agent  -n a1  -c  /opt/apps/flume-1.9.0/conf/ -f  /opt/apps/flume-1.9.0/agents/04_demo.prop   -Dflume.monitoring.type=http -Dflume.monitoring.port=34545  1>/dev/null  2>&1  &
   #  sleep  10   注意启动一个进程后  进程的启动需要时间 , 检查是否成功应该等一会
  fi
done
